package com.yanqu.road.logcollector.export;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.utils.classutil.ScanningFileHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.util.*;

public class ExportCKTempExcel {

    public static void main(String[] args) {
        String packageName = "com.yanqu.road.entity.log";
        ScanningFileHelper scanningFileHelper = new ScanningFileHelper();
        List<Class> classes = scanningFileHelper.searchClasses(packageName, true);
        List<ExportCKTempData> exportCKTempDataList = new ArrayList<>();

        // 首行
        ExportCKTempData firstData = new ExportCKTempData("tableName", "platformName", "fields", "requiredOptions", "remark");
        exportCKTempDataList.add(firstData);

        for (Class<?> cls : classes) {
            FieldTypeAnnotation clsTypeAnnotation = cls.getAnnotation(FieldTypeAnnotation.class);
            if (clsTypeAnnotation == null || !clsTypeAnnotation.autoCreateTable()) {
                continue;
            }
            if (clsTypeAnnotation != null && !clsTypeAnnotation.uploadCk()) {
                // 不需要保存
                continue;
            }
            ExportCKTempData exportCKTempData = new ExportCKTempData();

            if (StringUtils.isNullOrEmpty(clsTypeAnnotation.desc())) {
                exportCKTempData.setPlatformName(clsTypeAnnotation.tableName());
            } else {
                exportCKTempData.setPlatformName(clsTypeAnnotation.desc());
            }
            // 表名
            exportCKTempData.setTableName(clsTypeAnnotation.tableName() + "_dst");

            // 解析字段
            List<Field> allField = new ArrayList<>();
            // 递归收集所有字段，并记录声明顺序
            collectFieldsWithIndex(cls, allField);

            // 创建 SortedMap 并填充数据
            Map<String, String> fieldMap = new LinkedHashMap<>();
            for (Field field : allField) {
                FieldTypeAnnotation fieldTypeAnnotation = field.getAnnotation(FieldTypeAnnotation.class);
                if (fieldTypeAnnotation == null) {
                    continue;
                }
                String dbName = field.getName();
                if (!StringUtils.isNullOrEmpty(fieldTypeAnnotation.columnName())) {
                    dbName = fieldTypeAnnotation.columnName();
                }
                fieldMap.put(dbName, fieldTypeAnnotation.desc());
            }
            exportCKTempData.setFields(JSON.toJSONString(fieldMap));

            exportCKTempData.setRequiredOptions("{}");
            exportCKTempDataList.add(exportCKTempData);
        }
        // 获取项目根目录
        String projectRoot = System.getProperty("user.dir");
        // 源代码包路径
        String packagePath = "GameLogCollector\\src\\main\\java\\com\\yanqu\\road\\logcollector\\export";
        // 构建目标文件路径
        String targetFilePath = projectRoot + File.separator + packagePath + File.separator + "ck.xlsx";
        EasyExcel.write(targetFilePath, ExportCKTempData.class).sheet("游戏日志").doWrite(exportCKTempDataList);
    }

    private static void collectFieldsWithIndex(Class<?> cls, List<Field> allField) {
        if (cls == null) {
            return;
        }
        // 记录当前类的字段
        allField.addAll(Arrays.asList(cls.getDeclaredFields()));

        // 递归收集父类的字段
        collectFieldsWithIndex(cls.getSuperclass(), allField);
    }

}
